package com.openbravo.pos.panels;

import java.util.List;

import javax.swing.JPanel;
import javax.swing.BoxLayout;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductCategory;
import com.openbravo.data.basic.BasicException;
import cn.ekuma.data.ui.swing.dnd.AbstractJBeanDropPanel;
import com.openbravo.pos.base.AppLocal;

import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.epos.db.table.I_Product;

public class JDropProductByCategoryPanel extends JPanel {
	I_DataLogic dlSales ;
	/**
	 * Create the panel.
	 */
	public JDropProductByCategoryPanel(final I_DataLogic dlSales2) {
		initComponents();
		this.dlSales=dlSales2;
		add(new AbstractJBeanDropPanel<ProductCategory,Product>(){
			@Override
			public void doDropInsert(List<Product> beans) {
					try {
						for(Product p:beans){			
							dlSales2.updateByFieldWithIKeyed(p, I_Product.CATEGORY, bean.getID());
							p.setCategoryID(bean.getID());
						  }
						} catch (BasicException e) {
							e.printStackTrace();
						}
			}

			public void doDropDelete(List<Product> dropList) {
				for(Product p:dropList){
						try {
						dlSales2.updateByFieldWithIKeyed(p, I_Product.CATEGORY, null);
						p.setCategoryID(bean.getID());
						} catch (BasicException e) {
							e.printStackTrace();
						}
				     }
				 }
					
			
			@Override
			public String readerBean(ProductCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.prodcategory");
			}
		});
		add(new AbstractJBeanDropPanel<ProductCategory,Product>(){
			@Override
			public void doDropInsert(List<Product> beans) {
			
					try {
						for(Product p:beans){			
							dlSales2.updateByFieldWithIKeyed(p, I_Product.CATEGORY, bean.getID());
							p.setCategoryID(bean.getID());
						  }
						} catch (BasicException e) {
							e.printStackTrace();
						}
			}
			
			public void doDropDelete(List<Product> dropList) {
				for(Product p:dropList){
						try {
						dlSales2.updateByFieldWithIKeyed(p, I_Product.CATEGORY, null);
						p.setCategoryID(bean.getID());
						} catch (BasicException e) {
							e.printStackTrace();
						}
				     }
				 }

			@Override
			public String readerBean(ProductCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.prodcategory");
			}
			
		});
		add(new AbstractJBeanDropPanel<ProductCategory,Product>(){
			@Override
			public void doDropInsert(List<Product> beans) {
			
					try {
						for(Product p:beans){			
							dlSales2.updateByFieldWithIKeyed(p, I_Product.CATEGORY, bean.getID());
							p.setCategoryID(bean.getID());
						  }
						} catch (BasicException e) {
							e.printStackTrace();
						}
			}
			
			public void doDropDelete(List<Product> dropList) {
				for(Product p:dropList){
						try {
						dlSales2.updateByFieldWithIKeyed(p, I_Product.CATEGORY, null);
						p.setCategoryID(bean.getID());
						} catch (BasicException e) {
							e.printStackTrace();
						}
				     }
				 }

			@Override
			public String readerBean(ProductCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.prodcategory");
			}
			
		});
		add(new AbstractJBeanDropPanel<ProductCategory,Product>(){
			@Override
			public void doDropInsert(List<Product> beans) {
			
					try {
						for(Product p:beans){			
							dlSales2.updateByFieldWithIKeyed(p, I_Product.CATEGORY, bean.getID());
							p.setCategoryID(bean.getID());
						  }
						} catch (BasicException e) {
							e.printStackTrace();
						}
			}

			public void doDropDelete(List<Product> dropList) {
				for(Product p:dropList){
						try {
						dlSales2.updateByFieldWithIKeyed(p, I_Product.CATEGORY, null);
						p.setCategoryID(bean.getID());
						} catch (BasicException e) {
							e.printStackTrace();
						}
				     }
			}
			
			@Override
			public String readerBean(ProductCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.prodcategory");
			}
			
		});
		
	}
	private void initComponents() {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
	}
	
	public String getTabName(){
		return AppLocal.getIntString("label.prodcategory");
	}

}
